load("@npm_angular_bazel//:index.bzl", "ng_module")
load("@build_bazel_rules_nodejs//:defs.bzl", "rollup_bundle")
load("@npm_bazel_typescript//:defs.bzl", "ts_devserver")
load("@npm_bazel_karma//:defs.bzl", "ts_web_test_suite")
load("@io_bazel_rules_sass//sass:sass.bzl", "sass_binary", "sass_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])  # Apache License 2.0

exports_files(["tsconfig.app.json"])

ng_module(
    name = "app_lib",
    srcs = ["main.ts"],
    assets = [
        "sched.html",
    ],
    deps = [
        "//client/app:app_root",
        "@npm//@angular/core",
        "@npm//@angular/platform-browser",
    ],
)

sass_library(
    name = "material_theming",
    srcs = ["@npm//:node_modules/@angular/material/_theming.scss"],
)

sass_binary(
    name = "material-theme",
    src = "material-theme.scss",
    deps = [
        ":material_theming",
    ],
)

# We always strip these paths off the front of any assets we serve
_ROOT_DIRS = [
    "npm/node_modules/zone.js/dist",
    "npm/node_modules/d3/dist",
]

ts_devserver(
    name = "devserver",
    # serve these files rooted at /
    additional_root_paths = _ROOT_DIRS,
    bootstrap = [
        "@npm//:node_modules/pako/dist/pako.min.js",
    ],
    # Start from the development version of the main
    entry_module = "schedviz/client/main",
    # <script> and <link> tags will be automatically injected into this index file
    index_html = "index.html",
    # Serve these files in addition to the JavaScript bundle
    # The corresponding <script> or <link> tags will be injected into the index_html

    # These scripts will be included in the JS bundle after require.js
    # They should have only named UMD modules, or require.js will throw.
    scripts = [
        ":module-id.js",
        ":pako_requirejs",
        ":zonejs_testing_requirejs",
        ":d3_requirejs",
        ":rxjs_umd_modules",
        "@npm//:node_modules/tslib/tslib.js",
    ],
    static_files = [
        ":material-theme",
        "@npm//:node_modules/zone.js/dist/zone.min.js",
        "@npm//:node_modules/hammerjs/hammer.min.js",
        "@npm//:node_modules/pako/dist/pako.min.js",
    ],
    # Tell Bazel to build the sources first
    deps = [
        ":app_lib",
    ],
)

genrule(
    name = "pako",
    srcs = [
        "@npm//:node_modules/pako/dist/pako.min.js",
    ],
    outs = [
        "pako.min.js",
    ],
    cmd = """cat $< >> $@""",
)

# Create a dummy zone.js AMD module no errors are thrown when encountering the prod zone import.
genrule(
    name = "zonejs_testing_requirejs",
    srcs = [
        "@npm//:node_modules/zone.js/dist/zone.min.js",
    ],
    outs = [
        "zone_testing.js",
    ],
    cmd = """echo "define('zone.js/dist/zone.min.js', function(require, exports, module) {});" > $@""",
)

# Convert d3 to an AMD module so it can be loaded in the RequireJS test environment.
genrule(
    name = "d3_requirejs",
    srcs = [
        "@npm//:node_modules/d3/dist/d3.js",
    ],
    outs = [
        "d3.js",
    ],
    cmd = """echo "define('d3', function(require, exports, module) {" > $@ \
             && cat $< >> $@ \
             && echo '});' >> $@""",
)

# Convert pako to an AMD module so it can be loaded in the RequireJS test environment.
genrule(
    name = "pako_requirejs",
    srcs = [
        "@npm//:node_modules/pako/dist/pako.min.js",
    ],
    outs = [
        "pako.js",
    ],
    cmd = """echo "define('pako/dist/pako.min.js', function(require, exports, module) {" > $@ \
             && cat $< >> $@ \
             && echo '});' >> $@""",
)

genrule(
    name = "module_id_txt",
    srcs = [
        ":module-id.js",
    ],
    outs = [
        "module_id.txt",
    ],
    cmd = "cat $(SRCS) > $@",
)

rollup_bundle(
    name = "bundle",
    entry_point = ":main.ts",
    license_banner = ":module_id_txt",
    deps = [
        "app_lib",
        "@npm//@angular/common",
        "@npm//@angular/core",
        "@npm//@angular/material",
        "@npm//@angular/platform-browser",
        "@npm//@angular/router",
        "@npm//d3",
        "@npm//pako",
        "@npm//rxjs",
        "@npm//zone.js",
    ],
)

filegroup(
    name = "app_fileset",
    srcs = [
        "favicon.ico",
        "sched.html",
        ":bundle.js",
        ":material-theme",
        ":pako",
    ],
    path = "app_files",
)

filegroup(
    name = "rxjs_umd_modules",
    srcs = [
        ":rxjs_shims.js",
        "@npm//:node_modules/rxjs/bundles/rxjs.umd.js",
    ],
)

ts_web_test_suite(
    name = "unit_tests",
    srcs = [
        "@npm//:node_modules/tslib/tslib.js",
    ],
    # do not sort
    bootstrap = [
        "@npm//:node_modules/zone.js/dist/zone-testing-bundle.js",
        "@npm//:node_modules/reflect-metadata/Reflect.js",
        ":module-id.js",
        "@npm//:node_modules/pako/dist/pako.min.js",
    ],
    browsers = [
        "@io_bazel_rules_webtesting//browsers:chromium-local",
    ],
    deps = [
        ":d3_requirejs",
        ":pako_requirejs",
        ":rxjs_umd_modules",
        ":zonejs_testing_requirejs",
        "//client/app/collections:collections_tests",
        "//client/app/dashboard:dashboard_tests",
        "//client/app/heatmap:heatmap_tests",
        "//client/app/sidebar/thread_table:thread_table_tests",
    ],
)
